import requests from './request.js'
// import utils from './utils.js'

//接口方法
async function code(status, res, isShow) {
	// console.log(JSON.stringify(res,null,2))
	if (isShow) {
		uni.hideLoading();
	}
	if (status == 200001) {
		return res.data;
	} else if (status == 401001 || status == 400200) {
		if (!uni.getStorageSync("isLogin")) {
			// utils.showTips("请先登录账号")
			// utils.isgologin()
			uni.setStorageSync("isLogin", 1)
		}
	} else if (status == 810402 || status == 10001 || status == 100401) {
		// utils.showTips(res.data.message)
	} else {
		return res.data;
	}
}
async function showLoading(isShow) {
	if (isShow) {
		uni.showLoading({
			title: '加载中',
			mask: true
		});
	}
}
//登录
async function login(options, id, isShow) {
	showLoading(isShow)
	if (!options) {
		options = {};
	}
	var [error, res] = await uni.request(requests('auth/code2session', 'POST', options));
	// console.log("登录成功", res)
	// if (res.data.code == 200001) {
	// 	uni.removeStorage("isLogin")
	// }
	return code(res.data.code, res, isShow)
}
//图片资料
async function getimg(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('public/user-image?type=' + id , 'GET', options));
	return code(res.data.code, res, isShow)
}
//视频资料
async function getvideo(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('public/user-video', 'GET', options));
	return code(res.data.code, res, isShow)
}
// 获取学校列表
async function getSchoolList(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('public/user-school', 'GET', options));
	return code(res.data.code, res, isShow)
}
//年级报名费
async function usergrade(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('public/user-grade?parent_id=' + id, 'GET', options));
	return code(res.data.code, res, isShow)
}
// 班级
async function getclass(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('public/user-grade-class?grade_id=' + id, 'GET', options));
	return code(res.data.code, res, isShow)
}
//图片上传
async function upload(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('public/images/upload', 'POST', options));
	return code(res.data.code, res, isShow)
}
//协议
async function gettx(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('public/user-agreement', 'GET', options));
	return code(res.data.code, res, isShow)
}
//报名
async function signup(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('users/orders/create', 'POST', options));
	return code(res.data.code, res, isShow)
}
// 手机号
async function phonelogin(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('auth/phone-login', 'POST', options));
	return code(res.data.code, res, isShow)
}
// 用户信息
async function getuser(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('users/infos', 'GET', options));
	return code(res.data.code, res, isShow)
}
// 支付单号发送
async function pay(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('users/orders/'+ id +'/pay', 'POST', options));
	return code(res.data.code, res, isShow)
}
// 支付
async function wxpay(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('pay/wechat/notify', 'POST', options));
	return code(res.data.code, res, isShow)
}
// 用户协议接口
async function getuserxy(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('public/about-us', 'GET', options));
	return code(res.data.code, res, isShow)
}
// 隐私政策
async function getuserys(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('public/customer-phone', 'GET', options));
	return code(res.data.code, res, isShow)
}


// code换openID
async function code2session(options, id, isShow) {
	showLoading(isShow)
	if (!options) {options = {}}
	var [error, res] = await uni.request(requests('auth/code2session', 'POST', options));
	return code(res.data.code, res, isShow)
}
module.exports ={
	login, //登录
	getimg, //图片资料
	getvideo, // 视频资料
	usergrade,//年级报名费
	getSchoolList,//学校
	getclass, //班级
	upload, //图片上传
	gettx, // 协议
	signup, //报名
	phonelogin, // 手机号
	getuser, //获取信息
	pay,
	wxpay,
	getuserxy,//用户协议接口
	getuserys,//隐私政策
	code2session,// code换openID
}